const app = getApp();
import { addFavorite, delFavorite } from '../../../utils/requestApi.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsInfo: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgUrl: app.data.imgurl,
  },

  ready(){
    
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onAdd(e) {
      wx.setStorageSync('ctype', e.currentTarget.dataset.ctype)
      this.triggerEvent('open');
    },

    onCollect(e) {
      let { goodsInfo } = this.data;
      if (e.currentTarget.dataset.status == 'true') {
        delFavorite({
          ProductId: goodsInfo.DefaultSku.ProductId,
          ShopType: 2
        }).then(res => {
          if (res.data.Status == 'Success') {
            goodsInfo.IsFavorite = 'false'
            this.setData({ goodsInfo })
          }
          wx.showToast({ icon: 'none', title: res.data.Message })
        })
      } else {
        addFavorite({
          ProductId: goodsInfo.DefaultSku.ProductId,
          ShopType: 2
        }).then(res => {
          if (res.data.Status == 'Success') {
            goodsInfo.IsFavorite = 'true'
            this.setData({ goodsInfo })
          }
          wx.showToast({ icon: 'none', title: res.data.Message })
        })
      }
    }
  },
})
